package br.com.gps;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.DecimalFormat;

import android.R.integer;
import android.app.Activity;
import android.app.Service;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.TextView;

public class GPSActivity extends Activity implements LocationListener {
	/** Called when the activity is first created. */
	
	String latitude;
	String longitude;
	int velocidade;
	String datahora;

	LocationManager locationManager;
	TextView informacoes;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		informacoes = (TextView) this.findViewById(R.id.textInfo);

		locationManager = (LocationManager) this
				.getSystemService(LOCATION_SERVICE);

		locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
				1000, 10, this);
	}

	@Override
	public void onLocationChanged(Location location) {
		String dados = String
				.format("Latitude:\t %f\nLongitude:\t %f\nVelocidade:\t %f\nPrecisao:\t %f",
						location.getLatitude(), location.getLongitude(),
						location.getSpeed() * 3.6, location.getAccuracy());
		this.informacoes.setText(dados);

		Localizacao localizacao = new Localizacao();
		localizacao.setLatitudeAtual(String.valueOf(location.getLatitude()));
		localizacao.setLongitudeAtual(String.valueOf(location.getLongitude()));
		localizacao
				.setVelocidadeAtual(String.valueOf(location.getSpeed() * 3.6));

		localizacao.setLatitudeAtual(localizacao.getLatitudeAtual().substring(
				0, 9));
		localizacao.setLongitudeAtual(localizacao.getLongitudeAtual()
				.substring(0, 10));

		/*
		 * latitude = String.valueOf(location.getLatitude()); longitude =
		 * String.valueOf(location.getLongitude());/*
		 */
		String vel = String.valueOf(location.getSpeed() * 3.6);

		if (location.hasSpeed() && (vel != null) && (vel != "")
				) {

			velocidade = (int) (location.getSpeed() * 3.6);/**/

			localizacao.setVelocidadeAtual(String.valueOf(velocidade));
		} else {
			localizacao.setVelocidadeAtual("0");
		}

		// Exemplo URL Server Home Page PHP
		// "http://192.168.1.3/PHPAnalyzerMobli/index.php"ipServer + ":" +
		// portaServer+
		// http://192.168.1.123
		try {
			Fachada.getInstance().salvar(localizacao);
		} catch (Exception e) {
			// TODO: handle exception
		}

	}

	@Override
	public void onProviderDisabled(String provider) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onProviderEnabled(String provider) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onStatusChanged(String provider, int status, Bundle extras) {
		// TODO Auto-generated method stub

	}

}